package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.paymentsheet.pojo.dto.PaymentSheetGetDTO;
import com.xbongbong.pro.paymentsheet.pojo.vo.PaymentSheetGetVO;
import com.xbongbong.saas.service.PaymentSheetService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 回款单
 * @author feng.zheng
 * @version v1.0
 * @date 2019/3/14 17:27
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/paymentSheet")
public class PaymentSheetController {

    @Resource
    private PaymentSheetService paymentSheetService;

//    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public String add(@RequestBody @Valid PaymentSheetSaveDTO paymentSheetSaveDTO, BindingResult br) {
//        XbbResponse<FormDataAddVO> response;
//        if (br.hasErrors()) {
//            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
//            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
//        } else {
//            try {
//                FormDataAddVO formDataAddVO = paymentSheetService.add(paymentSheetSaveDTO);
//                response = new XbbResponse<>(formDataAddVO);
//            } catch (XbbException e) {
//                response = new XbbResponse<>(e);
//            }
//        }
//        return JSON.toJSONString(response);
//    }

    @RequestMapping(value = "/paymentSheet/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getPaymentSheet(@RequestBody @Valid PaymentSheetGetDTO paymentSheetSaveDTO, BindingResult br) {
        XbbResponse<PaymentSheetGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PaymentSheetGetVO paymentSheetGetVO = paymentSheetService.get(paymentSheetSaveDTO);
                response = new XbbResponse<>(paymentSheetGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
